package com.cavytech.wear;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class StateActivity extends Activity {

    byte mDeviceAddress[];

    final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
    public static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for ( int j = 0; j < bytes.length; j++ ) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_state);

        byte system[] = getIntent().getByteArrayExtra("SYSTEM");

        Button btn = (Button)findViewById(R.id.calibrate);
        btn.setOnClickListener(calbrateListener);

        TextView mTextViewData = (TextView)findViewById(R.id.DeviceState);
        mDeviceAddress = new byte[6];
        for ( int idx = 0; idx < 6; idx++ )
            mDeviceAddress[ idx ] = system[ idx ];
        mTextViewData.setText( //"Device address: " + bytesToHex( mDeviceAddress ) + "\n"
                        "Device address: " + Integer.toHexString(0xFF&system[0]) + ":"
                                + Integer.toHexString(0xFF&system[1]) + ":"
                                + Integer.toHexString(0xFF&system[2]) + ":"
                                + Integer.toHexString(0xFF&system[3]) + ":"
                                + Integer.toHexString(0xFF&system[4]) + ":"
                                + Integer.toHexString(0xFF&system[5])
                                + "\n"
                        + "Hardware version: " + system[ 10 ] + "\n"
                        + "Software version: " + system[ 11 ] + "\n"
        );

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK) {
            Intent intent=new Intent();
            intent.putExtra("RTN_CODE", 0);
            setResult(2, intent);
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private View.OnClickListener calbrateListener = new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent=new Intent();
            intent.putExtra("RTN_CODE", 1);
            setResult(2, intent);
            finish();
        }
    };
}
